﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Services;

namespace SdmxMailNotifier
{
    /// <summary>
    /// Summary description for Subscription
    /// </summary>
    [WebService(Namespace = "http://www.bfs.admin.ch/sms/sdmx-notification-service/2012/07/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Subscription : System.Web.Services.WebService
    {
        [WebMethod (Description = "This method returns the subscriptions of the specified user. ")]
        public DataTable getUserSubscriptions(string username)
        {
            // Load the user corresponding to the specified user name
            UsersManager users = (UsersManager)Application["Users"];
            return users.LoadUser(username, false);
        }

        [WebMethod(Description = "This method update the subscriptions of the specified user. " +
                                 "It returns true if and only if the user has been successfully updated.")]
        public bool updateUserSubscriptions(string username, DataTable subscriptions)
        {
            // Load the user corresponding to the specified user name
            UsersManager users = (UsersManager)Application["Users"];
            User user = users.LoadUser(username, true);

            if (user != null) // Check that the user has not already been loaded by another thread
            {
                try
                {
                    user.setSubscriptions(subscriptions);
                    return true;
                }
                finally
                {
                    // Release the user
                    users.ReleaseUser(user);
                }
            }
            return false;
        }

        [WebMethod(Description = "This method update the mail address of the specified user. " +
                                 "It returns true if and only if the user has been successfully updated.")]
        public bool updateUserMailAddress(string username, string mailAddress)
        {
            // Load the user corresponding to the specified user name
            UsersManager users = (UsersManager)Application["Users"];
            User user = users.LoadUser(username, true);

            if (user != null) // Check that the user has not already been loaded by another thread
            {
                try
                {
                    user.SetMailAddress(mailAddress);
                    return true;
                }
                finally
                {
                    // Release the user
                    users.ReleaseUser(user);
                }
            }
            return false;
        }

        [WebMethod(Description = "This method update the notification period of the specified user. " +
                                 "It returns true if and only if the user has been successfully updated.")]
        public bool updateUserNotificationPeriod(string username, string period)
        {
            // Load the user corresponding to the specified user name
            UsersManager users = (UsersManager)Application["Users"];
            User user = users.LoadUser(username, true);

            if (user != null) // Check that the user has not already been loaded by another thread
            {
                try
                {
                    user.SetPeriod(TimeSpan.Parse(period));
                    return true;
                }
                finally
                {
                    // Release the user
                    users.ReleaseUser(user);
                }
            }
            return false;
        }
    }
}
